package medical_application;
/** CSIS 343 Group 2 Course Project
 *  @authors Agnes Oliveira, Liane Heidemann, Daniel Schraufnagel, Matthew Lee
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class PatientGUI extends JFrame {
	private static final long serialVersionUID = 1L;
	JButton doneButton, intakeButton;
	JTextField fNameTextField, mNameTextField, lNameTextField, dobTextField,
			ageTextField, phoneTextField, addressTextField, insurTextField;
	JTextArea allergyTextArea, scriptTextArea;
	
	/**
	 * Constructor creates an empty patient record.
	 */
	public PatientGUI() {
		this.setSize(1010, 700);
		this.setResizable(false);
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.setTitle("P A T I E N T     R E C O R D");
		JPanel recordPanel = new JPanel();
		recordPanel.setLayout(new GridBagLayout());

		JLabel fNameLabel = new JLabel("First:");
		fNameLabel.setFont(Main.f);
		addToPanel(recordPanel, fNameLabel, 0, 0, 1, 1, GridBagConstraints.EAST);

		fNameTextField = new JTextField(15);
		fNameTextField.setFont(Main.f);
		addToPanel(recordPanel, fNameTextField, 1, 0, 1, 1,
				GridBagConstraints.WEST);

		JLabel mNameLabel = new JLabel("Middle:");
		mNameLabel.setFont(Main.f);
		addToPanel(recordPanel, mNameLabel, 2, 0, 1, 1, GridBagConstraints.EAST);

		mNameTextField = new JTextField(15);
		mNameTextField.setFont(Main.f);
		addToPanel(recordPanel, mNameTextField, 3, 0, 1, 1,
				GridBagConstraints.WEST);

		JLabel lNameLabel = new JLabel("Last:");
		lNameLabel.setFont(Main.f);
		addToPanel(recordPanel, lNameLabel, 5, 0, 1, 1, GridBagConstraints.EAST);

		lNameTextField = new JTextField(15);
		lNameTextField.setFont(Main.f);
		addToPanel(recordPanel, lNameTextField, 6, 0, 1, 1,
				GridBagConstraints.WEST);

		JLabel dobLabel = new JLabel("Date of Birth:");
		dobLabel.setFont(Main.f);
		addToPanel(recordPanel, dobLabel, 0, 1, 1, 1, GridBagConstraints.EAST);

		dobTextField = new JTextField(11);
		dobTextField.setFont(Main.f);
		addToPanel(recordPanel, dobTextField, 1, 1, 1, 1,
				GridBagConstraints.WEST);

		JLabel ageLabel = new JLabel("Age:");
		ageLabel.setFont(Main.f);
		addToPanel(recordPanel, ageLabel, 2, 1, 1, 1, GridBagConstraints.EAST);

		ageTextField = new JTextField(4);
		ageTextField.setFont(Main.f);
		addToPanel(recordPanel, ageTextField, 3, 1, 1, 1,
				GridBagConstraints.WEST);

		JLabel phoneLabel = new JLabel("Phone:");
		phoneLabel.setFont(Main.f);
		addToPanel(recordPanel, phoneLabel, 5, 1, 1, 1, GridBagConstraints.EAST);

		phoneTextField = new JTextField(15);
		phoneTextField.setFont(Main.f);
		addToPanel(recordPanel, phoneTextField, 6, 1, 1, 1,
				GridBagConstraints.WEST);

		JLabel addressLabel = new JLabel("Address:");
		addressLabel.setFont(Main.f);
		addToPanel(recordPanel, addressLabel, 0, 2, 1, 1,
				GridBagConstraints.EAST);

		addressTextField = new JTextField(66);
		addressTextField.setFont(Main.f);
		addToPanel(recordPanel, addressTextField, 1, 2, 7, 1,
				GridBagConstraints.WEST);

		JLabel insurLabel = new JLabel("Insurance:");
		insurLabel.setFont(Main.f);
		addToPanel(recordPanel, insurLabel, 0, 3, 1, 1, GridBagConstraints.EAST);

		insurTextField = new JTextField(23);
		insurTextField.setFont(Main.f);
		addToPanel(recordPanel, insurTextField, 1, 3, 2, 1,
				GridBagConstraints.WEST);

		JLabel allergyLabel = new JLabel("Allergies:");
		allergyLabel.setFont(Main.f);
		addToPanel(recordPanel, allergyLabel, 0, 4, 1, 1,
				GridBagConstraints.NORTHEAST);

		allergyTextArea = new JTextArea(7, 30);
		allergyTextArea.setFont(Main.f);
		JScrollPane allergyScrollPane = new JScrollPane(allergyTextArea,
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		addToPanel(recordPanel, allergyScrollPane, 1, 4, 3, 4,
				GridBagConstraints.NORTHWEST);

		JLabel scriptLabel = new JLabel("Prescriptions:");
		scriptLabel.setFont(Main.f);
		addToPanel(recordPanel, scriptLabel, 0, 8, 1, 1,
				GridBagConstraints.NORTHEAST);

		scriptTextArea = new JTextArea(7, 30);
		scriptTextArea.setFont(Main.f);
		JScrollPane scriptScrollPane = new JScrollPane(scriptTextArea,
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		addToPanel(recordPanel, scriptScrollPane, 1, 8, 9, 8,
				GridBagConstraints.NORTHWEST);

		doneButton = new JButton("Done");
		doneButton.setFont(Main.f);
		addToPanel(recordPanel, doneButton, 6, 9, 1, 1,
				GridBagConstraints.SOUTHEAST);

		intakeButton = new JButton("Intake");
		intakeButton.setFont(Main.f);
		addToPanel(recordPanel, intakeButton, 0, 9, 1, 1,
				GridBagConstraints.SOUTHWEST);

		actionIntake();
		actionDone();
		this.add(recordPanel);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}// end of default constructor, empty patient record

	/**
	 * This constructor instantiates the patient record GUI and displays the
	 * selected patient information
	 * @param a valid patient object       
	 * @return a patient record with data fields containing patient information
	 */
	public PatientGUI(Patient obj) {
		this.setSize(1010, 700);
		this.setResizable(false);
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.setTitle("P A T I E N T     R E C O R D");
		JPanel recordPanel = new JPanel();
		recordPanel.setLayout(new GridBagLayout());

		JLabel fNameLabel = new JLabel("First:");
		fNameLabel.setFont(Main.f);
		addToPanel(recordPanel, fNameLabel, 0, 0, 1, 1, GridBagConstraints.EAST);

		fNameTextField = new JTextField(15);
		fNameTextField.setFont(Main.f);
		fNameTextField.setText(obj.getFirst());
		addToPanel(recordPanel, fNameTextField, 1, 0, 1, 1,
				GridBagConstraints.WEST);

		JLabel mNameLabel = new JLabel("Middle:");
		mNameLabel.setFont(Main.f);
		addToPanel(recordPanel, mNameLabel, 2, 0, 1, 1, GridBagConstraints.EAST);

		mNameTextField = new JTextField(15);
		mNameTextField.setFont(Main.f);
		mNameTextField.setText(obj.getMiddle());
		addToPanel(recordPanel, mNameTextField, 3, 0, 1, 1,
				GridBagConstraints.WEST);

		JLabel lNameLabel = new JLabel("Last:");
		lNameLabel.setFont(Main.f);
		addToPanel(recordPanel, lNameLabel, 5, 0, 1, 1, GridBagConstraints.EAST);

		lNameTextField = new JTextField(15);
		lNameTextField.setFont(Main.f);
		lNameTextField.setText(obj.getLast());
		addToPanel(recordPanel, lNameTextField, 6, 0, 1, 1,
				GridBagConstraints.WEST);

		JLabel dobLabel = new JLabel("Date of Birth:");
		dobLabel.setFont(Main.f);
		addToPanel(recordPanel, dobLabel, 0, 1, 1, 1, GridBagConstraints.EAST);

		dobTextField = new JTextField(11);
		dobTextField.setFont(Main.f);
		dobTextField.setText(obj.getDOB());
		addToPanel(recordPanel, dobTextField, 1, 1, 1, 1,
				GridBagConstraints.WEST);

		JLabel ageLabel = new JLabel("Age:");
		ageLabel.setFont(Main.f);
		addToPanel(recordPanel, ageLabel, 2, 1, 1, 1, GridBagConstraints.EAST);

		ageTextField = new JTextField(4);
		ageTextField.setFont(Main.f);
		ageTextField.setText(obj.getAge());
		addToPanel(recordPanel, ageTextField, 3, 1, 1, 1,
				GridBagConstraints.WEST);

		JLabel phoneLabel = new JLabel("Phone:");
		phoneLabel.setFont(Main.f);
		addToPanel(recordPanel, phoneLabel, 5, 1, 1, 1, GridBagConstraints.EAST);

		phoneTextField = new JTextField(15);
		phoneTextField.setFont(Main.f);
		phoneTextField.setText(obj.getPhone());
		addToPanel(recordPanel, phoneTextField, 6, 1, 1, 1,
				GridBagConstraints.WEST);

		JLabel addressLabel = new JLabel("Address:");
		addressLabel.setFont(Main.f);
		addToPanel(recordPanel, addressLabel, 0, 2, 1, 1,
				GridBagConstraints.EAST);

		addressTextField = new JTextField(66);
		addressTextField.setFont(Main.f);
		addressTextField.setText(obj.getAddress());
		addToPanel(recordPanel, addressTextField, 1, 2, 7, 1,
				GridBagConstraints.WEST);

		JLabel insurLabel = new JLabel("Insurance:");
		insurLabel.setFont(Main.f);
		addToPanel(recordPanel, insurLabel, 0, 3, 1, 1, GridBagConstraints.EAST);

		insurTextField = new JTextField(23);
		insurTextField.setFont(Main.f);
		insurTextField.setText(obj.getInsurance());
		addToPanel(recordPanel, insurTextField, 1, 3, 2, 1,
				GridBagConstraints.WEST);

		JLabel allergyLabel = new JLabel("Allergies:");
		allergyLabel.setFont(Main.f);
		addToPanel(recordPanel, allergyLabel, 0, 4, 1, 1,
				GridBagConstraints.NORTHEAST);

		allergyTextArea = new JTextArea(7, 30);
		allergyTextArea.setFont(Main.f);
		JScrollPane allergyScrollPane = new JScrollPane(allergyTextArea,
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		allergyTextArea.setText(obj.getAllergies());
		addToPanel(recordPanel, allergyScrollPane, 1, 4, 3, 4,
				GridBagConstraints.NORTHWEST);

		JLabel scriptLabel = new JLabel("Prescriptions:");
		scriptLabel.setFont(Main.f);
		addToPanel(recordPanel, scriptLabel, 0, 8, 1, 1,
				GridBagConstraints.NORTHEAST);

		scriptTextArea = new JTextArea(7, 30);
		scriptTextArea.setFont(Main.f);
		JScrollPane scriptScrollPane = new JScrollPane(scriptTextArea,
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scriptTextArea.setText(obj.getScripts());
		addToPanel(recordPanel, scriptScrollPane, 1, 8, 9, 8,
				GridBagConstraints.NORTHWEST);

		doneButton = new JButton("Done");
		doneButton.setFont(Main.f);
		addToPanel(recordPanel, doneButton, 6, 9, 1, 1,
				GridBagConstraints.SOUTHEAST);

		intakeButton = new JButton("Intake");
		intakeButton.setFont(Main.f);
		addToPanel(recordPanel, intakeButton, 0, 9, 1, 1,
				GridBagConstraints.SOUTHWEST);

		actionDone();
		actionIntake();
		this.add(recordPanel);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}// end of alternate constructor, which will fill in data fields

	/**
	 * This method adds components to the JPanel using the GridBagLayout Manager
	 * and GridBag constraints
	 **/
	private void addToPanel(JPanel p, JComponent c, int x, int y, int width,
			int height, int align) {
		GridBagConstraints gc = new GridBagConstraints();
		gc.gridx = x;
		gc.gridy = y;
		gc.gridwidth = width;
		gc.gridheight = height;
		gc.weightx = 100;
		gc.weighty = 100;
		gc.insets = new Insets(2, 2, 2, 2);
		gc.anchor = align;
		gc.fill = GridBagConstraints.NONE;
		p.add(c, gc);
	}// end of addToPanel

	/**
	 * This method saves the data from patient record fields into a patient
	 * object, then into the patient Vector
	 */
	public void actionDone() {
		doneButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				JPasswordField strPassword = new JPasswordField(10);
				boolean flag = false;
				do {
					JOptionPane.showConfirmDialog(null, strPassword,
							"Enter Password:", JOptionPane.OK_CANCEL_OPTION);
					String pass = new String(strPassword.getPassword());
					if (pass.equals(LoginGUI.PASSWORD)) {
						String fname = fNameTextField.getText();
						String mname = mNameTextField.getText();
						String lname = lNameTextField.getText();
						String address = addressTextField.getText();
						String insure = insurTextField.getText();
						String allergy = allergyTextArea.getText();
						String script = scriptTextArea.getText();
						String dob = dobTextField.getText();
						String age = ageTextField.getText();
						String phone = phoneTextField.getText();
						Patient temp = new Patient(fname, mname,
								lname, address, insure, allergy, script, dob,
								age, phone);
						Read_Write_XML.patientVector.addElement(temp);
						flag = true;
						dispose();
					} else {
						JOptionPane.showMessageDialog(null,
								"Wrong login and/or password", "W R O N G",
								JOptionPane.ERROR_MESSAGE);
					}
				} while (!flag);
			}		
		});
	}// end of actionDone
	
	/**
	 *  This method handles the intakeButton when pressed it generates a blank Intake form.
	 */
	public void actionIntake() {
		intakeButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				new Intake();
			}
		});
	}// end of actionIntake
}// end of class